package _7_集合._1_List;


import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * List
 * https://www.liaoxuefeng.com/wiki/1252599548343744/1265112034799552
 *
 */
public class ListTest {


    /**
     * ArrayList 作用：需要增删元素的有序列表
     */

    /**
     * 删除、添加
     */
    @Test
    public void ArrayListTest1(){
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");

        list.remove(2);

        list.add(2,"Z");

        System.out.println(list);
    }

    /**
     * 可添加重复元素
     */
    @Test
    public void ArrayListTest2(){
        List<String> list = new ArrayList<>();
        list.add("apple");
        System.out.println("size : " + list.size());
        list.add("pear");
        System.out.println("size : " + list.size());
        list.add("apple");
        System.out.println("size : " + list.size());
        System.out.println(JSON.toJSONString(list));
    }

    /**
     * 允许添加Null
     */
    @Test
    public void ArrayListTest3(){
        List<String> list = new ArrayList<>();
        list.add("apple");
        System.out.println("size : " + list.size());
        list.add(null);
        System.out.println("size : " + list.size());
        list.add("pear");
        System.out.println("size : " + list.size());
        System.out.println(list.get(1));
        System.out.println(JSON.toJSONString(list));
    }

    /**
     * 遍历
     */
    @Test
    public void ArrayListTest4(){
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("pear");
        list.add("orage");

        System.out.println("遍历方法一：");
        for(int i = 0; i < list.size(); i++){
            System.out.print(list.get(i) + " ");
        }
        System.out.println("\n遍历方法二：");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println("\n遍历方法三：");
        for(String s : list){
            System.out.print(s + " ");
        }
    }

    /**
     * List和Array转换
     */
    @Test
    public void listToArray1(){
        List<String> list = Lists.newArrayList("apple","pear","orage");
        Object[] array = list.toArray();
        for (Object o: array) {
            System.out.print(o + " ");
        }
    }

    @Test
    public void listToArray2(){
        List<String> list = Lists.newArrayList("apple","pear","orage");
        String[] sArray = list.toArray(new String[list.size()]);
        for (String s: sArray) {
            System.out.print(s + " ");
        }
    }

    @Test
    public void listToArray3(){
        List<Integer> list = Lists.newArrayList(1,2,3,4,5);
        Number[] numbers = list.toArray(new Number[list.size()]);
        for (Number number: numbers) {
            System.out.print(number + " ");
        }
    }

//    @Test  //需要Java14
//    public void listToArray4(){
//        List<Integer> list = Lists.newArrayList(ReaderTest,2,3,4,5);
//        Integer[] integers = list.toArray(Integer[]::new);
//        for (Integer integer:integers) {
//            System.out.print(integer + " ");
//        }
//    }

//    @Test  //需要Java9
//    public void ArrayToList(){
//        Integer[] integers = {ReaderTest,2,3,4,5};
//        List<Integer> list = List.of(integers);
//        Iterator<Integer> iterator = list.iterator();
//        while(iterator.hasNext()){
//            System.out.print(iterator.next() + " ");
//        }
//    }


    /**
     *
     */

    @Test
    public void LinkedListTest(){

    }

    //练习

}
